WoWIndex = {
  available: undefined,
  drawIndexPage: function() {
    this.available = new Available();
    this.available.fetch( true );
  }
}

var Available = Class.create();
Available.prototype = {
  initialize: function() {
  },
  fetch: function( print ) {
    var this_copy = this;
    var myRequest = new Ajax.Request( 'ajax.php', {
      method: 'get',
      parameters: 'q=available',
      onSuccess: function( xmlhttp ) {
        try {
          var evaled = eval('('+xmlhttp.responseText+')');
          this_copy.list = evaled['Addons']['Addon'];
        }
        catch(e) {
          console.error(e);
        }
        if( print == true ) {
          this_copy.print();
        }
      }
    });
  },
  print: function() {
    var columns = ['Package','Notes','Author','Interface'];
    var myTable = document.createElement('table');
    myTable.id = 'addon-table';
    myTable.cellSpacing = 0;
    var headerRow = document.createElement('tr');
    columns.each( function( column ) {
      var columnElement = document.createElement('th');
      columnElement.innerHTML = column;
      headerRow.appendChild(columnElement);
    });
    myTable.appendChild(headerRow);
    var rowClass = 'light';
    if( this.list.length == undefined ) {
      this.list = [this.list];
    }
    this.list.each( function( addon ) {
      var myAddonRow = document.createElement('tr');
      myAddonRow.className = rowClass;
      rowClass = (rowClass == 'light') ? 'dark' : 'light';
        columns.each( function( field ) {
        var myCell = document.createElement('td');
        myCell.innerHTML = addon[field];
        myAddonRow.appendChild(myCell);
      });
      myTable.appendChild(myAddonRow);
    });
    document.body.appendChild(myTable);
  }
}
